package com.example.logic.chapter11;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

public class PriorityQueueDemo {
    private static final Comparator<Task> taskComparator = (o1, o2) -> {

        if (o1.getPriority() > o2.getPriority()) {
            return 1;
        } else if (o1.getPriority() < o2.getPriority()) {
            return -1;
        } else {
            return 0;
        }
    };

    public static void main(String[] args) {
        Queue<Task> tasks = new PriorityQueue<>(11);
        tasks.offer(new Task(20, "写日记"));
        tasks.offer(new Task(10, "看电视"));
        tasks.offer(new Task(100, "写代码"));
        Task task = tasks.poll();
        while (task != null) {
            System.out.print("处理任务： " + task.getName()
                    + "，优先级：" + task.getPriority() + "\n");
            task = tasks.poll();
        }
    }


    static class Task implements Comparable<Task> {
        /**
         * 优先级  值越大 优先级越高
         */
        int priority;
        String name;

        public Task(int priority, String name) {
            this.priority = priority;
            this.name = name;
        }

        public int getPriority() {
            return priority;
        }

        public String getName() {
            return name;
        }

        public void setPriority(int priority) {
            this.priority = priority;
        }

        public void setName(String name) {
            this.name = name;
        }


        @Override
        public int compareTo(Task o) {
            if (o.getPriority() > getPriority()) {
                return 1;
            } else if (o.getPriority() < getPriority()) {
                return -1;
            }
            return 0;
        }
    }
}
